package GUI;

import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Client_logic.Template;
import Client_logic.ConnectionToServer;
import Client_logic.Product;

/**
 * @This use for search for product.
 * @author ido
 * @author michael  
 * @author matan
 * @author sharon
 *
 */
public class ProductSearch extends JFrame
{
	private JLabel pCNumlLabel = null;
	private JButton searchButton = null;
	private JPanel jContentPane = null;
	private JComboBox pIDComboBox = null;
	private ProductForm productForm = null;
	private Product product = null;
	private long cnum = 0;
	private String initialitoForm;
	private TemplateForm templateform;
	private Template template = null;
	
	
	/**
	 * This is the default constructor
	 */
	public ProductSearch(String initialitoForm) 
	{
		super();
		this.initialitoForm = initialitoForm;
		initialize();		
	}
	
	/**
	 * This method initializes ProductForm
	 */
	private void initialize() 
	{
		product = new Product();
		this.setContentPane(getJContentPane());
		this.setTitle("PRODUCT SEARCH");
	}
	
	private JPanel getJContentPane() 
	{
		if (jContentPane == null) 
		{
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			
			pCNumlLabel = new JLabel();
			pCNumlLabel.setBounds(new Rectangle(15, 17, 113, 22));
			pCNumlLabel.setText("Catalogue number");
							
			jContentPane.setLayout(null);
			jContentPane.add(pCNumlLabel, null);
			jContentPane.add(getpIDComboBox(), null);
			jContentPane.add(getSearchButton(),null);
			productForm = new ProductForm();
			productForm.getcancelButton().addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					setContentPane(jContentPane);								
				}
			});	
			
			templateform = new TemplateForm();
			templateform.getBackjButton().addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					setContentPane(jContentPane);						
				}
			});			
		}
		return jContentPane;		
	}
	
	/**
	 * This method initializes PIDComboBox	
	 */
	private JComboBox getpIDComboBox()
	{
		if(pIDComboBox == null)
		{	
			pIDComboBox = new JComboBox();
			pIDComboBox.setBounds(new Rectangle(133, 17, 127, 25));
			long[] productid = null;
			try
			{
				ConnectionToServer connectiontoserver = new ConnectionToServer();		
				productid = (long[]) connectiontoserver.loadAllProductsID();
				if (productid == null)
				{
					JOptionPane.showMessageDialog(null, "Catalogue is EMPTY!!!");
					pIDComboBox.addItem(0);
				}	
				else
				{
					int len = productid.length;
					if (len > 0)
						cnum = productid[0];
					for(int i=0; i < len; i++)
					{
						pIDComboBox.addItem(productid[i]);
					}	
				}
			}catch(Exception e)
			{
				pIDComboBox.addItem(0);
			}
			pIDComboBox.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					cnum = (Long)pIDComboBox.getSelectedItem();	
				}	
			});	
		}	
		return pIDComboBox;
	}

	
	/**
	 * This method initializes searchButton	
	 */
	private JButton getSearchButton() 
	{
		if (searchButton == null) 
		{
			searchButton = new JButton();
			searchButton.setBounds(new Rectangle(90, 70, 130, 29));
			searchButton.setText("Search");
			searchButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					if (initialitoForm.equals("UpdateProduct"))
						initialiToUpdateProductForm();	
					if (initialitoForm.equals("removeProduct"))
						initialiToRemoveProductForm();	
					if (initialitoForm.equals("CreateTemplate"))
						initialiToCreateTemplateForm();
					
				}	
			});
		}
		return searchButton;
	}
	
	/**
	 * This method initializes to UpdateProductForm
	 */
	private void initialiToUpdateProductForm()
	{
		try 
		{
			if (cnum > 0)
			{
				if (product.loadProduct(cnum))
				{
					productForm.loadProduct(product);
					productForm.addUpdateButton();
					this.setTitle("PRODUCT UPDATE");
					setContentPane(productForm);
				}	
				else
					JOptionPane.showMessageDialog(null, "PRODUCT NOT EXIST IN DATA BASE");
			}
			else
				JOptionPane.showMessageDialog(null, "PLZ select a product");
		} catch (Exception excp)
		{
			JOptionPane.showMessageDialog(null, "ILEGAL INPUT");	
		}											
	}
	
	/**
	 * This method initializes to UpdateProductForm
	 */
	private void initialiToRemoveProductForm()
	{
		try 
		{
			if (cnum > 0)
			{
				if (product.loadProduct(cnum))
				{
					productForm.loadProduct(product);
					productForm.addRemoveButton();
					this.setTitle("PRODUCT REMOVE");
					setContentPane(productForm);
				}	
				else
					JOptionPane.showMessageDialog(null, "PRODUCT NOT EXIST IN DATA BASE");
			}
			else
				JOptionPane.showMessageDialog(null, "PLZ select a product");
		} catch (Exception excp)
		{
			JOptionPane.showMessageDialog(null, "ERROR");	
		}											
	}
	
	/**
	 * This method initializes to CreateTemplateForm
	 */
	private void initialiToCreateTemplateForm()
	{
		try 
		{
			if (cnum > 0)
			{
				if (product.loadProduct(cnum))
				{
					template = new Template(0,"",product,"","");
					templateform.loadTemplate(template);
					templateform.addCreateButton();
					this.setTitle("Create template");
					setContentPane(templateform);
				}	
				else
					JOptionPane.showMessageDialog(null, "PRODUCT NOT EXIST IN DATA BASE");
			}
			else
				JOptionPane.showMessageDialog(null, "PLZ select a product");
		} catch (Exception excp)
		{
			JOptionPane.showMessageDialog(null, "ERROR","",JOptionPane.ERROR_MESSAGE);	
		}											
	}
	
}
